﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ThanhNienSite
{
    public partial class Default : System.Web.UI.Page
    {
        private string Fullname
        {
            get { return (Session["Fullname"] ?? String.Empty).ToString(); }
            set { Session["Fullname"] = value; }
        }

        private string Email
        {
            get { return (Session["Email"] ?? String.Empty).ToString(); }
            set { Session["Email"] = value; }
        }

        private IList<SubjectCareer> Subjects
        {
            get
            {
                if (Session["Subjects"] == null)
                {
                    Session["Subjects"] = define.SubjectCareers;
                }
                return Session["Subjects"] as IList<SubjectCareer>;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.txtFullname.Text = "";
                this.txtEmail.Text = "";

                this.grpResult.Visible = false;
                this.rptSubjects.DataSource = null;
            }
            else
            {
                grpResult.Visible = true;
                if (Fullname.Equals(""))
                {
                    Fullname = this.txtFullname.Text;
                    Email = this.txtEmail.Text;
                    TblUserManager.CreateInstant().InsertUpdate(-1, Fullname, Email);
                }
                if (this.rptSubjects.Items.Count > 0)
                {
                    for (int i = 0; i < Subjects.Count; i++)
                    {
                        GridView dtg = (GridView)this.rptSubjects.Items[i].FindControl("dtgQuestions");
                        for (int j = 0; j < dtg.Rows.Count; j++)
                        {
                            QuestionCareer question = Subjects[i].Questions[j];
                            question.FirstCheck = (dtg.Rows[j].FindControl("chkFirstCheck") as CheckBox).Checked;
                            question.SecondCheck = (dtg.Rows[j].FindControl("chkSecondCheck") as CheckBox).Checked;
                            question.ThirdCheck = (dtg.Rows[j].FindControl("chkThirdCheck") as CheckBox).Checked;
                            question.FourthCheck = (dtg.Rows[j].FindControl("chkFourthCheck") as CheckBox).Checked;
                            question.FifthCheck = (dtg.Rows[j].FindControl("chkFifthCheck") as CheckBox).Checked;
                        }
                    }
                }
                this.rptSubjects.DataSource = Subjects;
                this.rptSubjects.DataBind();
            }
        }

        protected void rptSubjects_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item ||
                e.Item.ItemType == ListItemType.AlternatingItem)
            {
                GridView dtg = (GridView)e.Item.FindControl("dtgQuestions");
                SubjectCareer o = e.Item.DataItem as SubjectCareer;

                dtg.Columns[0].HeaderText = o.SubjectName;
                o.SubTotal = o.Questions.Sum(x => x.Total);
                dtg.Columns[6].FooterText = o.SubTotal.ToString();
                dtg.DataSource = o.Questions;
                dtg.DataBind();

                Label lit = (Label)e.Item.FindControl("litFooter");
                lit.Text = o.Footer;
            }
        }

        protected void dtg_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                //Build custom header.
                GridView oGridView = (GridView)sender;
                GridViewRow oGridViewRow = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);

                TableCell oTableCell = new TableCell();

                oTableCell.Text = e.Row.Cells[0].Text;
                oTableCell.RowSpan = 2;
                oTableCell.Font.Bold = true;
                oTableCell.HorizontalAlign = HorizontalAlign.Left;
                oGridViewRow.Cells.Add(oTableCell);

                oTableCell = new TableCell();
                oTableCell.Text = "Mức độ đúng với tôi";
                oTableCell.ColumnSpan = 5;
                oGridViewRow.Cells.Add(oTableCell);

                oTableCell = new TableCell();
                oTableCell.Text = "Điểm";
                oTableCell.RowSpan = 2;
                oGridViewRow.Cells.Add(oTableCell);

                e.Row.Cells.RemoveAt(0);
                e.Row.Cells.RemoveAt(5);
                oGridView.Controls[0].Controls.AddAt(0, oGridViewRow);
            }

            if (e.Row.RowType == DataControlRowType.Footer)
            {
                for (int i = 0; i < 5; i++)
                {
                    e.Row.Cells.RemoveAt(0);
                }
                e.Row.Cells[0].ColumnSpan = 6;
                e.Row.Cells[0].Text = "Tổng cộng";
            }
        }

        protected void btnResult_Click(object sender, EventArgs e)
        {
            this.litResult.Text = Compare();
            Session["Fullname"] = null;
            Session["Email"] = null;
            Session["Subjects"] = null;
        }

        string Compare()
        {
            string result = String.Empty;

            ReverserClass reverserClass = new ReverserClass();
            string[] subjects = define.Subjects.Clone() as string[];
            int[] scores = Subjects.Select(x => x.SubTotal).ToArray<int>();
            System.Array.Sort(scores, subjects, reverserClass);

            int index = Array.IndexOf(define.Subjects, subjects[0]);
            result += String.Format("<b>{0}:</b> {1} điểm.<p>{2}</p>", subjects[0], scores[0], define.Results[index]);
            index = Array.IndexOf(define.Subjects, subjects[1]);
            result += String.Format("<b>{0}:</b> {1} điểm.<p>{2}</p>", subjects[1], scores[1], define.Results[index]);

            return result;
        }
    }
}